<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use app\common\trait\SettingsTrait;
use FormBuilder\Factory\Elm;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageService;
use plugin\ycMassage\app\model\MassageServiceCategory;
use plugin\ycMassage\app\model\MassageServiceImage;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\State;
use support\Request;
use think\facade\Db;

class ServiceController extends BaseController
{
    use SettingsTrait;

    /**
     * 应用ID（null则获取系统配置）
     * @var int|null
     * @author 贵州猿创科技有限公司
     * @email 416716328@qq.com
     */
    protected $saas_appid = null;

    /**
     * 构造函数
     * @return void
     * @author 贵州猿创科技有限公司
     * @copyright 贵州猿创科技有限公司
     * @email 416716328@qq.com
     */
    public function initialize()
    {
        parent::initialize();
        $this->saas_appid = $this->request->saas_appid;
    }
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('name', 'input', '项目', [
            'placeholder' => '项目搜索'
        ]);
        $builder->addScreen('cid', 'select', '分类', [
            'placeholder' => '分类搜索',
            'options' => MassageServiceCategory::getOptions($request->saas_appid),
            'clearable' => true,
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addTopButton('add', '添加项目', [
            'api'       => $this->pluginPrefix . '/admin/Service/create',
            'path' => '/Service/create',
            'type'      => 'modal'
        ], [
            'title' => '添加项目',
        ], [
            'type' => 'success'
        ]);
        $builder->addRightButton('examine', '审核', [
            'api'    => $this->pluginPrefix . '/admin/Service/examine',
            'path' => '/Service/examine',
            'type'      => 'modal',
            'params' => [
                'field' => 'examine',
                'where' => '=',
                'value' => Examine::WAIT['value']
            ]
        ], [], [
            'type' => 'danger',
            'link' => true
        ]);
        $builder->addRightButton('update', '修改', [
            'api'    => $this->pluginPrefix . '/admin/Service/update',
            'path' => '/Service/examine',
            'type'      => 'modal'
        ], [], [
            'type' => 'primary',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumn('cate_title', '分类', [
                'width' => 100
            ])
            ->addColumn('title', '项目', [])
            ->addColumnEle('thumb', '封面', [
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('origin_price', '原价', [
                'width' => 100
            ])
            ->addColumn('price', '价格', [
                'width' => 100
            ])
            ->addColumn('material_price', '材料费', [
                'width' => 100
            ])
            ->addColumn('sales_num', '销量', [
                'width' => 100
            ])
            ->addColumn('service_duration', '服务时长', [
                'width' => 100
            ])
            ->addColumn('technician_commission', '技师佣金(%)', [
                'width' => 100
            ])
            ->addColumn('mch_commission', '商户佣金(%)', [
                'width' => 100
            ])
            ->addColumnEle('state', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'switch',
                    'api' => $this->pluginPrefix . '/admin/Service/updateState',
                    'checked' => State::YES,
                    'unchecked' => State::NO
                ],
            ]);
        $builder->addColumnEle('overtime_state', '加钟开关', [
            'width' => 100,
            'params' => [
                'type' => 'switch',
                'api' => $this->pluginPrefix . '/admin/Service/updateState',
                'checked' => State::YES,
                'unchecked' => State::NO
            ],
        ]);
        $builder->addColumn('overtime_rule', '加钟规则', [
            'width' => 100
        ]);
        $builder->addColumnEle('examine', '审核状态', [
            'width' => 100,
            'params' => [
                'type' => 'tags',
                'options' => Examine::dictOptions(),
                'style' => Examine::dictOptions('style')
            ],
        ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['s.saas_appid', '=', $request->saas_appid]
        ];
        $cid = $request->get('cid');
        if ($cid) {
            $where[] = ['s.cid', '=', $cid];
        }
        $name = $request->get('name');
        if ($name) {
            $where[] = ['s.title', 'like', "%{$name}%"];
        }
        $examine = $request->get('examine', Examine::YES['value']);
        if ($examine !== null) {
            $where[] = ['s.examine', '=', $examine];
        }
        $field = [
            's.*',
            'cate.title as cate_title'
        ];
        $data = MassageService::where($where)
            ->alias('s')
            ->join('MassageServiceCategory cate', 's.cid=cate.id', 'LEFT')
            ->order('s.create_time desc')
            ->field($field)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->overtime_rule = '无';
                if ($item->overtime_state) {
                    if ($item->overtime_separate) {
                        $item->overtime_rule = "{$item->overtime_price}元/{$item->overtime_duration}分钟";
                    } else {
                        $item->overtime_rule = "跟随项目定价";
                    }
                }
            });
        return $this->successRes($data);
    }
    public function examineList(Request $request)
    {
        return $this->index($request);
    }
    public function examineListGetTable(Request $request)
    {
        return $this->indexGetTable($request);
    }
    public function create(Request $request)
    {
        $saas_appid = $request->saas_appid;
        if ($request->method() === 'POST') {
            $D = $request->post();
            if (empty($D['thumb'])) {
                return $this->fail('请上传项目图片');
            }
            Db::startTrans();
            try {
                $MassageService = new MassageService;
                $D['saas_appid'] = $saas_appid;
                $D['thumb'] = $D['thumb'][0];
                if (!empty($D['video'])) {
                    $D['video'] = $D['video'][0];
                }
                if ($D['service_duration'] < 1) {
                    throw new \Exception('项目时长不能小于1分钟');
                }
                $MassageService->save($D);
                if (!empty($D['thumbs'])) {
                    # 过滤空值
                    $D['thumbs'] = array_filter($D['thumbs']);
                    if(empty($D['thumbs'])){
                        throw new \Exception('请上传项目轮播图');
                    }
                    $MassageServiceImageThumbs = new MassageServiceImage;
                    $MassageServiceImageThumbs->action = 'thumbs';
                    $MassageServiceImageThumbs->service_id = $MassageService->id;
                    $MassageServiceImageThumbs->saas_appid = $saas_appid;
                    $MassageServiceImageThumbs->images = $D['thumbs'];
                    $MassageServiceImageThumbs->save();
                }
                if (!empty($D['content'])) {
                    $D['content'] = array_filter($D['content']);
                    if(empty($D['content'])){
                        throw new \Exception('请上传项目介绍');
                    }
                    $MassageServiceImageContent = new MassageServiceImage;
                    $MassageServiceImageContent->action = 'content';
                    $MassageServiceImageContent->service_id = $MassageService->id;
                    $MassageServiceImageContent->saas_appid = $saas_appid;
                    $MassageServiceImageContent->images = $D['content'];
                    $MassageServiceImageContent->save();
                }
                if (!empty($D['taboo'])) {
                    $D['taboo'] = array_filter($D['taboo']);
                    if(!empty($D['taboo'])){
                        $MassageServiceImageTaboo = new MassageServiceImage;
                        $MassageServiceImageTaboo->action = 'taboo';
                        $MassageServiceImageTaboo->service_id = $MassageService->id;
                        $MassageServiceImageTaboo->saas_appid = $saas_appid;
                        $MassageServiceImageTaboo->images = $D['taboo'];
                        $MassageServiceImageTaboo->save();
                    }
                }
                if (!empty($D['order'])) {
                    $D['order'] = array_filter($D['order']);
                    if(!empty($D['order'])){
                        $MassageServiceImageOrder = new MassageServiceImage;
                        $MassageServiceImageOrder->action = 'order';
                        $MassageServiceImageOrder->service_id = $MassageService->id;
                        $MassageServiceImageOrder->saas_appid = $saas_appid;
                        $MassageServiceImageOrder->images = $D['order'];
                        $MassageServiceImageOrder->save();
                    }
                }
                Db::commit();
                return $this->success('添加成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $this->getFormBuilder($builder, $saas_appid);
        $data = $builder->create();
        return $this->successRes($data);
    }
    public function update(Request $request)
    {
        $id = $request->get('id');
        $saas_appid = $request->saas_appid;
        $MassageService = MassageService::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageService) {
            return $this->fail('数据不存在');
        }
        $MassageServiceImageThumbs = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'thumbs', 'saas_appid' => $request->saas_appid])->find();
        $MassageServiceImageContent = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'content', 'saas_appid' => $request->saas_appid])->find();
        $MassageServiceImageTaboo = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'taboo', 'saas_appid' => $request->saas_appid])->find();
        $MassageServiceImageOrder = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'order', 'saas_appid' => $request->saas_appid])->find();
        if ($request->method() === 'POST') {
            $D = $request->post();
            Db::startTrans();
            try {
                if ($D['service_duration'] < 1) {
                    throw new \Exception('项目时长不能小于1分钟');
                }
                if (empty($D['video'])) {
                    unset($D['video']);
                    $MassageService->video = null;
                } else {
                    $D['video'] = $D['video'][0];
                }
                $MassageService->save($D);
                if (!empty($D['thumbs'])) {
                    $D['thumbs'] = array_filter($D['thumbs']);
                    if(empty($D['thumbs'])){
                        throw new \Exception('请上传项目轮播图');
                    }
                    if (!$MassageServiceImageThumbs) {
                        $MassageServiceImageThumbs = new MassageServiceImage;
                        $MassageServiceImageThumbs->action = 'thumbs';
                        $MassageServiceImageThumbs->service_id = $MassageService->id;
                        $MassageServiceImageThumbs->saas_appid = $saas_appid;
                    }
                    $MassageServiceImageThumbs->images = $D['thumbs'];
                    $MassageServiceImageThumbs->save();
                }
                if (!empty($D['content'])) {
                    $D['content'] = array_filter($D['content']);
                    if(empty($D['content'])){
                        throw new \Exception('请上传项目介绍');
                    }
                    if (!$MassageServiceImageContent) {
                        $MassageServiceImageContent = new MassageServiceImage;
                        $MassageServiceImageContent->action = 'content';
                        $MassageServiceImageContent->service_id = $MassageService->id;
                        $MassageServiceImageContent->saas_appid = $saas_appid;
                    }
                    $MassageServiceImageContent->images = $D['content'];
                    $MassageServiceImageContent->save();
                }
                if (!empty($D['taboo'])) {
                    $D['taboo'] = array_filter($D['taboo']);
                    if (!$MassageServiceImageTaboo) {
                        $MassageServiceImageTaboo = new MassageServiceImage;
                        $MassageServiceImageTaboo->action = 'taboo';
                        $MassageServiceImageTaboo->service_id = $MassageService->id;
                        $MassageServiceImageTaboo->saas_appid = $saas_appid;
                    }
                    $MassageServiceImageTaboo->images = $D['taboo'];
                    $MassageServiceImageTaboo->save();
                }
                if (!empty($D['order'])) {
                    $D['order'] = array_filter($D['order']);
                    if (!$MassageServiceImageOrder) {
                        $MassageServiceImageOrder = new MassageServiceImage;
                        $MassageServiceImageOrder->action = 'order';
                        $MassageServiceImageOrder->service_id = $MassageService->id;
                        $MassageServiceImageOrder->saas_appid = $saas_appid;
                    }
                    $MassageServiceImageOrder->images = $D['order'];
                    $MassageServiceImageOrder->save();
                }
                Db::commit();
                return $this->success('审核成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }
        $MassageService->thumb = [$MassageService['thumb']];
        if (!empty($MassageService['video'])) {
            $MassageService->video = [$MassageService['video']];
        }
        $MassageService->thumbs = [];
        $MassageService->content = [];
        $MassageService->taboo = [];
        $MassageService->order = [];
        if ($MassageServiceImageThumbs) {
            $MassageService->thumbs = $MassageServiceImageThumbs->images;
        }
        if ($MassageServiceImageContent) {
            $MassageService->content = $MassageServiceImageContent->images;
        }
        if ($MassageServiceImageTaboo) {
            $MassageService->taboo = $MassageServiceImageTaboo->images;
        }
        if ($MassageServiceImageOrder) {
            $MassageService->order = $MassageServiceImageOrder->images;
        }
        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $this->getFormBuilder($builder, $request->saas_appid);
        $data = $builder->setData($MassageService)->create();
        return $this->successRes($data);
    }
    private function getFormBuilder($builder, $saas_appid)
    {
        $builder->addRow('title', 'input', '项目名称', '', [
            'col'           => 12
        ]);
        $builder->addRow('cid', 'select', '所属分类', '', [
            'col'           => 12,
            'options'       => MassageServiceCategory::getOptions($saas_appid)
        ]);
        $builder->addRow('tips', 'input', '项目副标题', '', [
            'col'           => 12
        ]);
        $builder->addComponent('thumb', 'uploadify', '项目封面', '', [
            'col' => 6,
            'props' => [
                'type' => 'image',
                'limit' => 1,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $builder->addComponent('video', 'uploadify', '视频介绍', '', [
            'col' => 6,
            'props' => [
                'type' => 'video',
                'limit' => 1,
                'format' => ['mp4', 'avi']
            ],
        ]);
        $builder->addRow('origin_price', 'number', '项目原价格', null, [
            'col'           => 8,
            'props' => [
                'precision' => 2,
                'controls' => false,
                'min' => 0
            ]
        ]);
        $builder->addRow('price', 'number', '项目价格', null, [
            'col'           => 8,
            'props' => [
                'precision' => 2,
                'controls' => false,
                'min' => 0
            ]
        ]);
        $builder->addRow('material_price', 'number', '原料费用', null, [
            'col'           => 8,
            'props' => [
                'precision' => 2,
                'controls' => false,
                'min' => 0
            ]
        ]);
        $builder->addRow('technician_commission', 'number', '技师提成(%)', null, [
            'col'           => 8,
            'props' => [
                'precision' => 0,
                'controls' => false,
                'min' => 0,
                'max' => 100
            ]
        ]);
        $builder->addRow('mch_commission', 'number', '商户提成(%)', null, [
            'col'           => 8,
            'props' => [
                'precision' => 0,
                'controls' => false,
                'min' => 0,
                'max' => 100
            ]
        ]);
        $builder->addRow('service_duration', 'number', '服务时长(分钟)', null, [
            'col'           => 8,
            'props' => [
                'precision' => 0,
                'controls' => false,
                'min' => 0
            ]
        ]);
        $builder->addRow('overtime_state', 'radio', '是否可以加钟', State::YES['value'], [
            'col' => 6,
            'options' => State::getOptions(),
            'control' => [
                [
                    'value' => State::YES['value'],
                    'where' => '==',
                    'rule' => [
                        Elm::radio('overtime_separate', '自定义加钟价格', State::NO['value'])
                            ->options(State::getOptions())
                            ->col([
                                'span' => 6
                            ])
                            ->control([
                                [
                                    'value' => State::YES['value'],
                                    'where' => '==',
                                    'rule' => [
                                        Elm::number('overtime_duration', '加钟时长(分钟)')
                                            ->col([
                                                'span' => 6
                                            ])
                                            ->props([
                                                'precision' => 0,
                                                'controls' => false,
                                                'min' => 0
                                            ])
                                    ],
                                ],
                                [
                                    'value' => State::YES['value'],
                                    'where' => '==',
                                    'rule' => [
                                        Elm::number('overtime_price', '加钟价格')
                                            ->col([
                                                'span' => 6
                                            ])
                                            ->props([
                                                'precision' => 2,
                                                'controls' => false,
                                                'min' => 0
                                            ])
                                    ],
                                ],
                            ])
                    ]
                ]
            ]
        ]);
        $builder->addRow('state', 'radio', '状态', State::YES['value'], [
            'col' => 24,
            'options' => State::getOptions()
        ]);
        $builder->addRow('sales_num', 'number', '销量', null, [
            'col'           => 8,
            'props' => [
                'precision' => 0,
                'controls' => false,
                'min' => 0
            ]
        ]);
        $builder->addRow('virtually_sales_num', 'number', '虚拟销量', null, [
            'col'           => 8,
            'props' => [
                'precision' => 0,
                'controls' => false,
                'min' => 0
            ]
        ]);
        $builder->addRow('sort', 'number', '排序', 255, [
            'col'           => 8,
            'props' => [
                'precision' => 0,
                'controls' => false,
                'min' => 0,
                'max' => 255
            ]
        ]);
        $builder->addRow('examine', 'radio', '审核状态', Examine::YES['value'], [
            'col' => 24,
            'options' => Examine::getOptions()
        ]);
        $builder->addComponent('thumbs', 'uploadify', '项目轮播图', '', [
            'col' => 24,
            'props' => [
                'type' => 'image',
                'limit' => 9,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $builder->addComponent('content', 'uploadify', '项目介绍', '', [
            'col' => 24,
            'props' => [
                'type' => 'image',
                'limit' => 9,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $builder->addComponent('taboo', 'uploadify', '禁忌说明', '', [
            'col' => 24,
            'props' => [
                'type' => 'image',
                'limit' => 9,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $builder->addComponent('order', 'uploadify', '下单须知', '', [
            'col' => 24,
            'props' => [
                'type' => 'image',
                'limit' => 9,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
    }
    public function examine(Request $request)
    {
        $id = $request->get('id');
        $MassageService = MassageService::where(['id' => $id, 'saas_appid' => $request->saas_appid, 'examine' => Examine::WAIT['value']])->find();
        if (!$MassageService) {
            return $this->fail('数据不存在');
        }
        if ($request->method() === 'POST') {
            $D = $request->post();
            $MassageService->examine = $D['examine'];
            Db::startTrans();
            try {
                if (!in_array($MassageService->examine, [Examine::YES['value'], Examine::NO['value']])) {
                    throw new \Exception('请选择审核状态');
                }
                if ($MassageService->examine == Examine::NO['value']) {
                    if (empty($D['message'])) {
                        throw new \Exception('请填写驳回理由');
                    }
                    $MassageService->message = $D['message'];
                } else {
                    $MassageService->message = '通过';
                }
                $MassageService->save();
                Db::commit();
                return $this->success('审核成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');

        $builder->addComponent('id', 'info', 'ID', '', [
            'col'           => 6,
        ]);
        $builder->addComponent('title', 'info', '项目', '', [
            'col'           => 6,
        ]);
        $builder->addComponent('tips', 'info', '项目副标题', '', [
            'col'           => 12,
        ]);
        if ($MassageService->cid) {
            $builder->addComponent('cate_title', 'info', '项目分类', MassageServiceCategory::where(['id' => $MassageService->cid])->value('title'), [
                'col'           => 6,
            ]);
        }
        $builder->addComponent('thumb', 'el-image', '封面', '', [
            'col'           => 6,
            'props'         => [
                'src'       => $MassageService->thumb,
                'width'     => 160,
                'height'    => 160
            ]
        ]);
        $builder->addComponent('video_text', 'info', '视频介绍', $MassageService->video ? '有' : '', [
            'col'           => 6,
        ]);
        $builder->addDivider('服务价格');
        $builder->addComponent('price', 'info', '价格', '', [
            'col'           => 6
        ]);
        $builder->addComponent('origin_price', 'info', '原价格', '', [
            'col'           => 6
        ]);
        $builder->addComponent('material_price', 'info', '物料价格', '', [
            'col'           => 6
        ]);
        $builder->addComponent('service_duration', 'info', '服务时长', '', [
            'col'           => 6
        ]);
        $builder->addComponent('technician_commission', 'info', '技师分成(%)', '', [
            'col'           => 6
        ]);
        $builder->addComponent('mch_commission', 'info', '商户分成(%)', '', [
            'col'           => 6
        ]);
        $builder->addComponent('overtime', 'info', '加钟', $MassageService->overtime_state ? State::YES['text'] : State::NO['text'], [
            'col'           => 6
        ]);
        if ($MassageService->overtime_state) {
            $rule = '';
            if ($MassageService->overtime_separate) {
                $rule = "{$MassageService->overtime_price}元/{$MassageService->overtime_duration}分钟";
            } else {
                $rule = "跟随项目定价";
            }
            $builder->addComponent('overtime1', 'info', '加钟规则', $rule, [
                'col'           => 6
            ]);
        }
        $thumbs = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'thumbs', 'saas_appid' => $request->saas_appid])->find();
        if ($thumbs) {
            $builder->addDivider('服务轮播图');
            foreach ($thumbs->images as $key => $url) {
                $builder->addComponent('thumb_' . $key, 'el-image', '', '', [
                    'col'           => 4,
                    'props'         => [
                        'src'       => $url,
                        'width'     => 160,
                        'height'    => 160
                    ]
                ]);
            }
        }
        $content = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'content', 'saas_appid' => $request->saas_appid])->find();
        if ($content) {
            $builder->addDivider('服务介绍');
            foreach ($content->images as $key => $url) {
                $builder->addComponent('content_' . $key, 'el-image', '', '', [
                    'col'           => 4,
                    'props'         => [
                        'src'       => $url,
                        'width'     => 160,
                        'height'    => 160
                    ]
                ]);
            }
        }
        $taboo = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'taboo', 'saas_appid' => $request->saas_appid])->find();
        if ($taboo) {
            $builder->addDivider('服务禁忌');
            foreach ($taboo->images as $key => $url) {
                $builder->addComponent('taboo_' . $key, 'el-image', '', '', [
                    'col'           => 4,
                    'props'         => [
                        'src'       => $url,
                        'width'     => 160,
                        'height'    => 160
                    ]
                ]);
            }
        }
        $order = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'order', 'saas_appid' => $request->saas_appid])->find();
        if ($order) {
            $builder->addDivider('下单须知');
            foreach ($order->images as $key => $url) {
                $builder->addComponent('order_' . $key, 'el-image', '', '', [
                    'col'           => 4,
                    'props'         => [
                        'src'       => $url,
                        'width'     => 160,
                        'height'    => 160
                    ]
                ]);
            }
        }
        $builder->addDivider('审核');
        $builder->addRow('examine', 'radio', '状态', Examine::YES['value'], [
            'col' => 24,
            'options' => Examine::getOptions(),
            'control' => [
                [
                    'value' => Examine::NO['value'],
                    'where' => '==',
                    'rule' => [
                        Elm::textarea('message', '驳回理由')
                            ->col([
                                'span' => 24
                            ])
                            ->props([
                                'filterable' => true,
                                'placeholder' => '请输入驳回理由',
                            ])
                    ],
                ],
            ]
        ]);
        $data = $builder->setData($MassageService)->create();
        return $this->successRes($data);
    }
    public function updateState(Request $request)
    {
        $id = $request->post('id');
        $field = $request->post('field');
        $MassageService = MassageService::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        $MassageService->{$field} = $MassageService->{$field} == State::YES['value'] ? State::NO['value'] : State::YES['value'];
        if ($MassageService->save()) {
            return $this->success('修改成功');
        }
        return $this->fail('修改失败');
    }
}
